import { checkValueType } from "../tool/object";
import { rules as InnerRules } from "./rules";


let newId = null;
export function checkNew (id) {
    //用于处理 修改过后在新增 watch监听数据必填(显示红字)的bug
    newId = id;
    setTimeout(() => {
        newId = 10086;
    }, 500)
}

export const initValidator = (vm, rules, errors) => {
    console.log("初始化本地表单验证，规则：", rules, errors)
    Object.keys(rules).forEach(field => {
        //   vm.$watch(field,{
        //     handler: value => check.call(this,vm, rules[field], field, value,errors),
        //     immediate:false
        // })

        vm.$watch(field, function (newVal, oldVal) {
            if (newId) {
                check.call(this, vm, rules[field], field, newVal, errors);
            } else {
                return false;
            }
        }, {
            immediate: false
        })
    })
}

export const check = function (vm, rule, field, value, errors) {
    console.log("1.checkValueType(Object,rule)", checkValueType("Object", rule), rule, field, value, errors)
    if (checkValueType("Object", rule)) {
        checkObjectRule.call(this, vm, rule, field, value, errors);
    } else if (checkValueType("Array", rule)) {
        rule.forEach(item => {
            let check = checkObjectRule.call(this, vm, item, field, value, errors);
            if (!check) {
                return false;
            }
        })
    }
}

const checkObjectRule = function (vm, rule, field, value, errors) {
    let { test, message } = rule;
    console.log("2.checkObjectRule", field, test, message);
    if (checkValueType("Function", test)) {
        console.log("3.!test(value)", !test(value), value)
        if (!test(value)) {
            // console.log("验证失败_",field);
            vm.$set(errors, field, message);
            // errors[field] = message;
            return false;
        } else {
            // delete errors[field];
            vm.$delete(errors, field);
            return true;
        }
    } else if (checkValueType("String", test)) {
        //获取 内置 校验规则
        let iRule = InnerRules[test];
        if (test == iRule.test) {
            console.error("校验规则自循环引用", test);
            return false;
        }
        checkObjectRule.call(this, vm, iRule, field, value, errors)
    } else if (checkValueType("RegExp", test)) {
        if (value && value.length > 0 && !test.test(value)) {
            vm.$set(errors, field, message);
            // errors[field] = message;
            return false;
        } else {
            // delete errors[field];
            vm.$delete(errors, field);
            return true;
        }
    } else {
        console.error("校验规则类型错误", test);
        return false;
    }
}


/**
 * 全部校验一次
 * @param {*} formRules 
 * @param {*} formPostData 
 * @param {*} errors 
 */
export const checkAll = (vm, formRules, formPostData, errors) => {
    console.log("checkAll_ing--------------")
    Object.keys(formRules).forEach(field => {
        console.log("checkAll_field", field)
        let arr = field.split(".");
        let code = "";
        if (arr && arr.length > 0) {
            code = arr[arr.length - 1];
        }
        console.log("checkAll_code", code)
        if (code) {
            let value = formPostData[code];
            check.call(this, vm, formRules[field], field, value, errors)
        }
    })
}
/**
 * 初始化 动态表单规则(暂不启用)
 * @param {*} onOffDefineFields 
 * @param {*} formRules 
 */
export const initRules = (onOffDefineFields, formRules) => {
    onOffDefineFields.forEach(f => {
        if ((f.dataType == 1 || f.dataType == 2) && f.onEdit) {
            let codeRules = new Array();
            if (f.onFill) {
                codeRules.push({ test: 'required' });
            }
            if (codeRules.length > 0) {
                formRules["formPostData." + f.code] = codeRules;
            }
        }
    });
}
